<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<body >
<head>
	<link rel="stylesheet" type="text/css" href="<c:url value='/resource/css/common.css'/>"/>
	<link rel="stylesheet"  href="<c:url value='/resource/css/zTreeStyle/zTreeStyle.css'/>" type="text/css"></link>
	<script type="text/javascript" src="<c:url value='/resource/js/jquery-1.7.2.min.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/js/jquery.ztree.core-3.2.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/js/jquery.ztree.excheck-3.2.min.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/artdialog/artdialog.js?skin=${sys_theme}'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/js/common.js'/>"></script>
	<script type="text/javascript" src="<c:url value='/resource/artdialog/iframetools.js'/>"></script>
	<script type="text/javascript">
		var setting = {
			async : {
				enable : true,
				url : "<c:url value='/dept/getAllDept.json'/>",
				dataType : "json",
				type : 'get',
				autoParam : [ "deptId" ],
				dataFilter : function(treeId, parentNode, childNodes) {
					return childNodes.data
				}
			},
			callback : {
				onClick : function(event, treeId, treeNode){
					var origin = artDialog.open.origin;
					origin.document.getElementById('deptname').value=treeNode.name;
					origin.document.getElementById('deptid').value=treeNode.deptId;
					art.dialog.close();
				},
				beforeExpand: beforeExpand,
				onExpand: onExpand,
				beforeDrag: beforeDrag,
				beforeDrop: beforeDrop
			},
			data : {
				key:{
					name:"alias"
				},
				simpleData : {
					enable : true,
					idKey : "deptId",
					pIdKey : "parentId",
					rootPId : 0
				}
			},
			edit:{
				enable:true,
				showRemoveBtn:false,
				showRenameBtn:false
			}
		};
		/**树初始化*/
		$(function() {
			$.fn.zTree.init($("#treeDemo"), setting);
			$("#treeDemo").css({"height":500});
		});
		
		/**拖拽事件*/
		function beforeDrag(treeId, treeNodes) {
			for (var i=0,l=treeNodes.length; i<l; i++) {
				if (treeNodes[i].drag === false) {
					return false;
				}
			}
			return true;
		}
		/**树节点拖拽前事件*/
		function beforeDrop(treeId, treeNodes, targetNode, moveType) {
			//判断节点是否可以拖拽
			var drogAble = targetNode ? targetNode.drop !== false : true;
			if(drogAble){
				if(treeNodes[0].parentId!=targetNode.parentId){
					art.dialog.alert("必须上级部门一致才能拖动");
					return false;
				}
				if(moveType=="inner"){
					art.dialog.alert("不可拖动到非同级部门");
					return false;
				}
				var flag = false;
					//后台修改
					$.ajax({
						type:"POST",
						url:"<%=request.getContextPath()%>/dept/changeorder.json",
						async:false,
						data:{
							'deptId':treeNodes[0].deptId,
							'orderNum':treeNodes[0].orderNum,
							'targetOrder':targetNode.orderNum
						},
						success:function(data){
							flag = true;
						}
					});
				return flag;
			}else{
				return false;
			}
		}
		/**同时只能展开一个节点，减少树的长度 start*/
		var curExpandNode = null;
		function beforeExpand(treeId, treeNode) {
			var pNode = curExpandNode ? curExpandNode.getParentNode():null;
			var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			for(var i=0, l=!treeNodeP ? 0:treeNodeP.children.length; i<l; i++ ) {
				if (treeNode !== treeNodeP.children[i]) {
					zTree.expandNode(treeNodeP.children[i], false);
				}
			}
			while (pNode) {
				if (pNode === treeNode) {
					break;
				}
				pNode = pNode.getParentNode();
			}
			if (!pNode) {
				singlePath(treeNode);
			}
	
		}
		function singlePath(newNode) {
			if (newNode === curExpandNode) return;
			if (curExpandNode && curExpandNode.open==true) {
				var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				if (newNode.parentTId === curExpandNode.parentTId) {
					zTree.expandNode(curExpandNode, false);
				} else {
					var newParents = [];
					while (newNode) {
						newNode = newNode.getParentNode();
						if (newNode === curExpandNode) {
							newParents = null;
							break;
						} else if (newNode) {
							newParents.push(newNode);
						}
					}
					if (newParents!=null) {
						var oldNode = curExpandNode;
						var oldParents = [];
						while (oldNode) {
							oldNode = oldNode.getParentNode();
							if (oldNode) {
								oldParents.push(oldNode);
							}
						}
						if (newParents.length>0) {
							for (var i = Math.min(newParents.length, oldParents.length)-1; i>=0; i--) {
								if (newParents[i] !== oldParents[i]) {
									zTree.expandNode(oldParents[i], false);
									break;
								}
							}
						} else {
							zTree.expandNode(oldParents[oldParents.length-1], false);
						}
					}
				}
			}
			curExpandNode = newNode;
		}
	
		function onExpand(event, treeId, treeNode) {
			curExpandNode = treeNode;
		}
</script>
<style type="text/css">
	body{scrollbar-track-color:#fff;scrollbar-3dlight-color:#BDCFE4;scrollbar-arrow-color:#BDCFE4;scrollbar-face-color:#fff;scrollbar-shadow-color:#fff;scrollbar-darkshadow-color:#BDCFE4;}
</style>
</head>
<body style="margin:0px;width:250px;height:500px;">
	<div class="zTreeDemoBackground left">
		<ul id="treeDemo" class="ztree" style="width:250px;margin:0px;padding:0px;"></ul>
	</div>
	<!-- 
	<div style="width:250px;margin:0px;padding:0px;overflow-x:hidden;overflow-x:hidden;overflow-y:auto;background-color:#f2f2f2;">
	</div>
	 -->
</body>
</html>